package com.share.shrimpcolo.shoot03.sync;

public class Clip {
	private int mBulletNum = 0;
	private int mClipNum = 3;
	
	public synchronized void add() {
		
		//System.out.println("Clip ==>1111 add mBulletNum = " +mBulletNum);
		
		while(mBulletNum > 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		//System.out.println("Clip ==>2222 add mBulletNum = " +mBulletNum);
		this.notifyAll();
		
		try{
			Thread.sleep(1000);
		}catch(InterruptedException exp) {
			exp.printStackTrace();
		}
		
		System.out.println("上弹夹, 枪内有12发子弹");
		mClipNum--;
		mBulletNum = 12;	
		//System.out.println("Clip ==>3333 add mBulletNum = " +mBulletNum);
	}
	
	public synchronized void use(){
		//System.out.println("===> Clip1111 use mBulletNum = " +mBulletNum);
		
		while(mBulletNum == 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		//System.out.println("===> Clip2222 use mBulletNum = " +mBulletNum);
		this.notifyAll();
		
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		mBulletNum--;
		//System.out.println("===> Clip333 use mBulletNum = " +mBulletNum);
		System.out.println("进行一次射击，还剩" + mBulletNum + "发子弹");
		
		if(mBulletNum == 0) {
			System.out.println("没有子弹了，请换弹夹");
		}
//		else if(mClipNum == 0) {
//			System.out.println("已经没有子弹了，射击完毕！！！");
//		}
	}//end use

	
	public synchronized int getmClipNum() {
		return mClipNum;
	}
	
	
	
	
	
}
